# -*- coding: utf-8 -*-
# @Organization  : asiainfo
# @Author        : 周伟东
# @Time          : 2021/1/12 14:15
# @Function      : kafka 生产者
from kafka import KafkaProducer


class Producer:
    # def __init__(self):
    #     import config
    #     bootstrap_servers = config.get_config('KAFKA_PRODUCER', 'bootstrap_servers')
    #     self.producer = KafkaProducer(bootstrap_servers=[bootstrap_servers])  # 连接kafka
    #     self.topic = config.get_config('KAFKA_PRODUCER', 'kafka_topic')
    #
    # def send_message(self, topic, message):
    #     self.producer.send(topic, message.encode('utf-8'))
    #
    # def send_result_message(self, message):
    #     self.send_message(self.topic, message)
    #
    # def close(self):
    #     self.producer.close()
    #
    # def flush(self):
    #     self.producer.flush()

    def send_result_message(self, message):
        import config
        bootstrap_servers = config.get_config('KAFKA_PRODUCER', 'bootstrap_servers')
        producer = KafkaProducer(bootstrap_servers=[bootstrap_servers])  # 连接kafka
        topic = config.get_config('KAFKA_PRODUCER', 'kafka_topic')
        producer.send(topic, message.encode('utf-8'))
        producer.flush()
        producer.close()


kafkaProducer = Producer()
